<template>
  <div>
    <el-form :inline="true" :model="search" class="demo-form-inline">
      <el-form-item label="搜索">
        <el-input v-model="search.name" placeholder="输入部门名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="search.address"
          placeholder="输入部门代码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      :data="resList"
      tooltip-effect="dark"
      style="width: 100%"
      border
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="nickname" label="部门名称"></el-table-column>
      <el-table-column prop="address" label="部门代码"></el-table-column>
      <el-table-column label="修改时间">
        <template slot-scope="scope">
          {{ scope.row.created_at | filterTime }}
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="修改人"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="set(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑弹框 -->
    <el-dialog title="修改" :visible.sync="flag">
      <el-form :model="form">
        <el-form-item label="部门名称">
          <el-input
            v-model="form.nickname"
            placeholder="输入部门名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门代码">
          <el-input
            v-model="form.address"
            placeholder="输入部门代码"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取消</el-button>
        <el-button type="primary" @click="update">确定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[5, 6, 7, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="list.length"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      listall: [],
      search: {
        name: "",
        address: "",
      },
      form: {
        nickname: "",
        address: "",
        uid: "",
      },
      flag: false,
      pageSize: 5,
      pageNum: 1,
    };
  },
  mounted() {
    this.$axios.get("/list.json").then((res) => {
      this.list = res.data;
      this.listall = res.data;
      console.log(this.listall);
    });
  },
  methods: {
    //搜索
    find() {
      this.list = this.listall.filter((item) => {
        return (
          item.nickname.includes(this.search.name) &&
          item.address.includes(this.search.address)
        );
      });
    },
    //删除
    del(i) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list.splice(i, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑回填数据
    set(val) {
      this.flag = true;
      this.form = val;
    },
    //保存编辑
    update() {
      this.flag = false;
      this.list.forEach((item) => {
        if (item.uid === this.form.uid) {
          item = this.form;
        }
      });
    },
    //每页条数
    handleSizeChange(val) {
      this.pageSize = val;
    },
    //页码
    handleCurrentChange(val) {
      this.pageNum = val;
    },
  },
  filters: {
    //时间过滤器
    filterTime(value) {
      var time = new Date(value * 1000);
      var year = time.getFullYear();
      var month = time.getMonth() + 1;
      var date = time.getDate();
      var hour = time.getHours();
      var mis = time.getMinutes();
      var sec = time.getSeconds();
      return `${year}-${month
        .toString()
        .padStart(2, "0")}-${date
        .toString()
        .padStart(2, "0")} ${hour
        .toString()
        .padStart(2, "0")}:${mis
        .toString()
        .padStart(2, "0")}:${sec.toString().padStart(2, "0")}`;
    },
  },
  computed: {
    resList() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
};
</script>
